package com.threeox.commonlibrary.util.request.model;

import android.content.Context;

import com.threeox.commonlibrary.entity.engine.request.RequestMsg;
import com.threeox.commonlibrary.entity.engine.request.base.BaseRequestMsg;
import com.threeox.commonlibrary.ui.view.loadview.ILoadDialog;
import com.threeox.commonlibrary.ui.view.loadview.LoadDialog;
import com.threeox.commonlibrary.util.DialogUtil;
import com.threeox.commonlibrary.util.request.RequestHelper;
import com.threeox.utillibrary.util.LogUtils;

/**
 *
 * @ClassName: RequestModel
 *
 * @Description: TODO()
 *
 * @author 赵屈犇
 *
 * @date 创建时间: 2017/12/12 9:53
 *
 * @version 1.0
 *
 */
public class RequestModel extends RequestHelper {

    protected Context mContext;
    protected RequestMsg mRequestMessage;
    // 是否显示请求中的Dialog
    protected boolean mIsShowDialog = false;

    protected ILoadDialog mLoadDialog = null;

    public static RequestModel newInstance(int resId) {
        return new RequestModel(resId, null);
    }

    public static RequestModel newInstance(int resId, Context context) {
        return new RequestModel(resId, context);
    }

    protected RequestModel(int resId, Context context) {
        super();
        this.mContext = context;
        mRequestMessage = mCommonConfig.getRequestMessage(resId);
    }

    /**
     * 执行请求
     */
    public void execRequest() {
        if (mContext == null) {
            LogUtils.e("请传入Context!");
        }
        if (mIsShowDialog && mContext != null) {
            mLoadDialog = DialogUtil.showLoadDialog(mLoadDialog, mRequestMessage.getLoadMessage(), mContext);
        }
        super.execRequest(mRequestMessage);
    }

    @Override
    protected void onSuccess(BaseRequestMsg requestMsg, String url, int id, String msg, Object data) {
        super.onSuccess(requestMsg, url, id, msg, data);
        DialogUtil.dismissDialog(mLoadDialog);
    }

    @Override
    protected void onError(BaseRequestMsg requestMsg, String url, int id, Object code, String msg, Object data) {
        super.onError(requestMsg, url, id, code, msg, data);
        DialogUtil.dismissDialog(mLoadDialog);
    }

    /**
     * 初始化所有请求参数
     *
     * @param data
     */
    public RequestModel initAllParams(Object data) {
        if (mRequestMessage != null) {
            mRequestMessage.initAllParams(data);
        }
        return this;
    }

    /**
     * 初始化代替参数
     *
     * @param data
     */
    public RequestModel initReplaceParam(Object data) {
        if (null != mRequestMessage) {
            mRequestMessage.initReplaceParam(data);
        }
        return this;
    }

    /**
     * 初始化请求参数
     *
     * @param data
     */
    public RequestModel initRequestParam(Object data) {
        if (null != mRequestMessage) {
            mRequestMessage.initRequestParam(data);
        }
        return this;
    }

    /**
     * 传入请求参数
     *
     * @param key
     * @param value
     */
    public RequestModel putRequestParam(String key, Object value) {
        if (null != mRequestMessage) {
            mRequestMessage.putRequestParam(key, value);
        }
        return this;
    }

    /**
     * 删除请求参数 根据key
     *
     * @param key
     */
    public RequestModel removeRequestParam(String key) {
        if (null != mRequestMessage) {
            mRequestMessage.removeRequestParam(key);
        }
        return this;
    }

    /**
     * 初始化请求头信息
     *
     * @param data
     */
    public RequestModel initRequestHeaders(Object data) {
        if (null != mRequestMessage) {
            mRequestMessage.initRequestHeaders(data);
        }
        return this;
    }

    /**
     * 初始化Sqlite 的操作参数
     *
     * @param data
     */
    public RequestModel initOpertionParams(Object data) {
        if (null != mRequestMessage) {
            mRequestMessage.initOpertionParams(data);
        }
        return this;
    }

    public RequestMsg getRequestMessage() {
        return mRequestMessage;
    }

    public RequestModel setShowDialog(boolean isShowDialog) {
        this.mIsShowDialog = isShowDialog;
        return this;
    }

    public void setContext(Context context) {
        this.mContext = context;
    }

    public ILoadDialog getLoadDialog() {
        if (mLoadDialog == null) {
            mLoadDialog = new LoadDialog(mContext);
        }
        return mLoadDialog;
    }
}
